//
//  HBEmptyTableViewController.m
//  HBLibary_Example
//
//  Created by Rambo on 2018/6/11.
//  Copyright © 2018年 Rambo. All rights reserved.
//

#import "HBEmptyTableViewController.h"
#import <HBLibary/HBLibary.h>

@interface HBEmptyTableViewController ()

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) HBDataSource *dataSource;

@end

@implementation HBEmptyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUI];
    
}

#pragma mark - 初始化

- (void)setUI{
    
    UIView *superView = self.view;
    static NSString *cellId = @"cellId";
    self.dataSource = [[HBDataSource alloc] initWithIdentifier:cellId tableViewDataSource:^(UITableViewCell *cell, NSString *model, NSIndexPath *indexPath) {
        cell.textLabel.text = model;
    }];
    
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        [superView addSubview:tableView];
        tableView.tableFooterView = [UIView new];
        tableView.dataSource = self.dataSource;
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
        tableView.hb_emptyView = [HBScrollEmptyView emptyWithImage:[UIImage imageNamed:@"icon_network_error"] title:@"没有数据" detail:@"这是一段描述" target:self action:@selector(changeData)];
        tableView;
    });
    
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"切换" style:UIBarButtonItemStylePlain target:self action:@selector(changeData)];
    
    self.navigationItem.rightBarButtonItem = bar;
}

#pragma mark - Action

- (void)changeData{
    if ([self.dataSource getAllModels].count > 0) {
        [self.dataSource removeAllModel];
    }else{
        NSMutableArray *titles = [NSMutableArray array];
        for (NSInteger index = 0; index < 30; index++) {
            NSString *title = [NSString stringWithFormat:@"这是标题%zd",index];
            [titles addObject:title];
        }
        [self.dataSource addModels:titles.copy];
    }
    [self.tableView reloadData];
    
}

@end
